package com.david.rocketmq;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @description: 消息队列制造者
 * @create: 2021-05-05-19-08
 * @author: David
 **/
@Getter
@Setter
@ToString
@Configuration
@Slf4j
public class Producer {

    private String groupName="miaosha";

    private String namesrvAddr = "localhost:9876";
    // 消息最大值
    private Integer maxMessageSize=4096;
    // 消息发送超时时间
    private Integer sendMsgTimeOut=3000;
    // 失败重试次数
    private Integer retryTimesWhenSendFailed=3;

    @PostConstruct
    @Bean
    public DefaultMQProducer defaultProducer() throws MQClientException {
        log.info("defaultProducer 正在创建---------------------------------------");
        DefaultMQProducer producer = new DefaultMQProducer(groupName);
        producer.setNamesrvAddr(namesrvAddr);
        producer.setVipChannelEnabled(false);
        producer.setMaxMessageSize(maxMessageSize);
        producer.setSendMsgTimeout(sendMsgTimeOut);
        producer.setRetryTimesWhenSendAsyncFailed(retryTimesWhenSendFailed);
        producer.start();
        log.info("rocketmq producer server 开启成功----------------------------------");
        return producer;
    }
}